package controllers;


import org.springframework.stereotype.*;
import models.Model;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.forwardMessages;
import views.html.index;
import views.html.startingGame;
import views.html.gameIndex;
import views.html.playedFold;
import views.html.playedCall;
import views.html.playedRaise;
import views.html.playedBet;
import views.html.playedCheck;
import views.html.playedAllIn;
import views.html.exchangeCards;
import views.html.nextGame;


@org.springframework.stereotype.Controller
public class Application extends Controller {

	private static ModelCollection models = new ModelCollection();
	private static CoreCollection cores = new CoreCollection();
	private static MsgCollection messages = new MsgCollection();
	private static int playerNr = 0;

	private static final String HOST = "127.0.0.1";
	private static final int PORT = 4444;

	public Application() {}

    public static Result index() {
        return ok(index.render("Welcome to Badugi Poker! Click \"JOIN GAME\" to play."));
    }
    
    //po wybraniu przez gracza "Join Game"
    public static Result joinGame() {
    	if(session("playerNr") != null) {
    		return ok(index.render("You're already in game Badugi Poker!"));
    	}
    	try {
    		Core tempCore = cores.newCore(playerNr);
    		tempCore.connectToServer(HOST, PORT);
    		Model tempModel = models.newModel(playerNr);
    		Messages tempMsg = messages.newMessages(playerNr);
    		tempModel.setOperationCode(tempCore.checkOperationCode());
    		//uruchomienie watku sluchacza
    		tempCore.getMessage();
			if (tempModel.getOperationCode().equals("01")) {
				tempModel.setParameters(tempCore.getParameters());
				System.out.println("PlayerID: " + tempModel.getPlayerID());
				session("playerNr", Integer.toString(playerNr));
			}
			tempModel.setText(tempCore.getMessageToDisplay());
			tempMsg.addMessage(tempModel.getText());
        	playerNr++;
			return redirect(routes.Application.startingGame());
		} catch (ConnectionFailureException e) {
			return ok(index.render("Connection failure. :( Try again later."));
		}
    }
    
    public static Result startingGame() {
    	if(session("playerNr") == null) {
    		return redirect(routes.Application.index());
    	}
		return ok(startingGame.render("Please wait for other players."));
    }

    public static Result beginGame() {
    	if(session("playerNr") == null) {
    		return redirect(routes.Application.index());
    	}
    	int playerNr = Integer.parseInt(session("playerNr"));
    	Core tempCore = cores.getCore(playerNr);
		Model tempModel = models.getModel(playerNr);
		Messages tempMsg = messages.getMessages(playerNr);
    	if(tempModel.isStartGameButtonEnabled() == false) {
    		return ok(startingGame.render("Please wait for other players."));
    	} else {
	    	tempCore.sendMessage("<" + tempModel.getPlayerID() + "><02><Y>");
	    	tempMsg.addMessage("Waiting for other players to start game.");
	    	return redirect(routes.Application.gameIndex());
    	}
    }
    
    public static Result gameIndex() {
    	if(session("playerNr") == null) {
    		return redirect(routes.Application.index());
    	}
		return ok(gameIndex.render("Game has started."));
    }
    
    public static Result playedCheck() {
    	int playerNr = Integer.parseInt(session("playerNr"));
		Core tempCore = cores.getCore(playerNr);
		Model tempModel = models.getModel(playerNr);
		Messages tempMsg = messages.getMessages(playerNr);
		if(tempModel.isBettingButtonsEnabled() == true) {
			if(tempModel.isCheckButtonEnabled() == true) {
				tempModel.setMessageToServer("<" + tempModel.getPlayerID() + "><06><CH,0>");
				tempCore.sendMessage(tempModel.getMessageToServer());
				System.out.println("aaa zalicytowano check: " + tempModel.getMessageToServer());
				//tempModel.disableBettingButtons();
				return ok(playedCheck.render("You want to play Check."));
			}
			else {
				return ok(playedCheck.render("You can not play Check. Choose another option."));
			}
		} else {
			return ok(playedCheck.render("Wait for your turn to bet."));
			//tempMsg.addMessage("Wait for your turn");
		}
    }
    
    public static Result playedBet(String betValue) {
    	int playerNr = Integer.parseInt(session("playerNr"));
		Core tempCore = cores.getCore(playerNr);
		Model tempModel = models.getModel(playerNr);
		Messages tempMsg = messages.getMessages(playerNr);
		if(tempModel.isBettingButtonsEnabled() == true) {
			if(tempModel.isBetButtonEnabled() == true) {
				tempModel.setMessageToServer("<" + tempModel.getPlayerID() + "><06><BE," + betValue +">");
				tempCore.sendMessage(tempModel.getMessageToServer());
				System.out.println("aaa zalicytowano bet: " + tempModel.getMessageToServer());
				//tempModel.disableBettingButtons();
				return ok(playedBet.render("You want to play Bet."));
			}
			else {
				return ok(playedBet.render("You can not play Bet. Choose another option."));
			}
		} else {
			return ok(playedBet.render("Wait for your turn to bet."));
			//tempMsg.addMessage("Wait for your turn");
		}
    }
    
    public static Result playedRaise(String raiseValue) {
    	int playerNr = Integer.parseInt(session("playerNr"));
		Core tempCore = cores.getCore(playerNr);
		Model tempModel = models.getModel(playerNr);
		Messages tempMsg = messages.getMessages(playerNr);
		if(tempModel.isBettingButtonsEnabled() == true) {
			if(tempModel.isRaiseButtonEnabled() == true) {
				tempModel.setMessageToServer("<" + tempModel.getPlayerID() + "><06><RA," + raiseValue +">");
				tempCore.sendMessage(tempModel.getMessageToServer());
				System.out.println("aaa zalicytowano raise: " + tempModel.getMessageToServer());
				//tempModel.disableBettingButtons();
				return ok(playedRaise.render("You want to play Raise."));
			}
			else {
				return ok(playedRaise.render("You can not play Raise. Choose another option."));
			}
		} else {
			return ok(playedRaise.render("Wait for your turn to bet."));
			//tempMsg.addMessage("Wait for your turn");
		}
    }
    
    public static Result playedCall() {
    	int playerNr = Integer.parseInt(session("playerNr"));
		Core tempCore = cores.getCore(playerNr);
		Model tempModel = models.getModel(playerNr);
		Messages tempMsg = messages.getMessages(playerNr);
		if(tempModel.isBettingButtonsEnabled() == true) {
			if(tempModel.isCallButtonEnabled() == true) {
				tempModel.setMessageToServer("<" + tempModel.getPlayerID() + "><06><CA," + tempModel.getCallValue() +">");
				tempCore.sendMessage(tempModel.getMessageToServer());
				System.out.println("aaa zalicytowano call: " + tempModel.getMessageToServer());
				//tempModel.disableBettingButtons();
				return ok(playedCall.render("You want to play Call."));
			}
			else {
				return ok(playedCall.render("You can not play Call. Choose another option."));
			}
		} else {
			return ok(playedCall.render("Wait for your turn to bet."));
			//tempMsg.addMessage("Wait for your turn");
		}
    }
    
    public static Result playedFold() {
    	int playerNr = Integer.parseInt(session("playerNr"));
		Core tempCore = cores.getCore(playerNr);
		Model tempModel = models.getModel(playerNr);
		Messages tempMsg = messages.getMessages(playerNr);
		if(tempModel.isBettingButtonsEnabled() == true) {
			tempModel.setMessageToServer("<" + tempModel.getPlayerID() + "><06><FO,0>");
			tempCore.sendMessage(tempModel.getMessageToServer());
			System.out.println("aaa zalicytowano fold: " + tempModel.getMessageToServer());
			//tempModel.disableBettingButtons();
			return ok(playedFold.render("You want to play Fold."));
		} else {
			return ok(playedFold.render("Wait for your turn to bet."));
			//tempMsg.addMessage("Wait for your turn");
		}
    }
    
    public static Result playedAllIn() {
    	int playerNr = Integer.parseInt(session("playerNr"));
		Core tempCore = cores.getCore(playerNr);
		Model tempModel = models.getModel(playerNr);
		Messages tempMsg = messages.getMessages(playerNr);
		if(tempModel.isBettingButtonsEnabled() == true) {
			if(tempModel.isAllInButtonEnabled() == true) {
				tempModel.setMessageToServer("<" + tempModel.getPlayerID() + "><06><AL," + tempModel.getPlayerBankrollNow() +">");
				tempCore.sendMessage(tempModel.getMessageToServer());
				System.out.println("aaa zalicytowano all-in: " + tempModel.getMessageToServer());
				//tempModel.disableBettingButtons();
				return ok(playedAllIn.render("You want to play All-In."));
			}
			else {
				return ok(playedAllIn.render("You can not play All-In. Choose another option."));
			}
		} else {
			return ok(playedAllIn.render("Wait for your turn to bet."));
			//tempMsg.addMessage("Wait for your turn");
		}
    }
    
    public static Result exchangeCards(String indexesOfSelectedCards) {
    	int playerNr = Integer.parseInt(session("playerNr"));
		Core tempCore = cores.getCore(playerNr);
		Model tempModel = models.getModel(playerNr);
		Messages tempMsg = messages.getMessages(playerNr);
		if(tempModel.isExchangeButtonEnabled() == true) {
			int nrOfSelectedCards = 0;
			if((tempModel.countNrOfSelectedCards(indexesOfSelectedCards)).equals("0")) {
				tempModel.setMessageToServer("<" + tempModel.getPlayerID() + "><08><0>");
				tempCore.sendMessage(tempModel.getMessageToServer());
			} else {
				tempModel.setMessageToServer("<" + tempModel.getPlayerID() + "><08><" + tempModel.countNrOfSelectedCards(indexesOfSelectedCards) + "," + indexesOfSelectedCards + ">");
				tempCore.sendMessage(tempModel.getMessageToServer());
			}
			System.out.println(tempModel.getMessageToServer());
			return ok(exchangeCards.render("You want to exchange Cards."));
		} else {
			return ok(exchangeCards.render("Wait for your turn to exchange."));
			//tempMsg.addMessage("Wait for your turn");
		}
    }
    
    public static Result nextGame() {
    	int playerNr = Integer.parseInt(session("playerNr"));
		Core tempCore = cores.getCore(playerNr);
		Model tempModel = models.getModel(playerNr);
		Messages tempMsg = messages.getMessages(playerNr);
		if(tempModel.isNextGameButtonEnabled() == true) {
			tempModel.setModelToDefault();
			tempCore.sendMessage("<0><10><Y>");
			System.out.println("<0><10><Y>");
			return ok(nextGame.render("Please wait for other players decision."));
		} else {
			return ok(nextGame.render("You can not do this. Wait until game ends."));
			//tempMsg.addMessage("Wait for your turn");
		}
    }

    //odebrano wiadomosc
	public static void messageReceived(int playerNr, String newMessageFromServer) {
		Core tempCore = cores.getCore(playerNr);
		Model tempModel = models.getModel(playerNr);
		Messages tempMsg = messages.getMessages(playerNr);
		try {
			tempCore.getMessage();
		} catch (ConnectionFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		switch (tempCore.checkOperationCode(newMessageFromServer)) {
		case "02":
			//asking player to start game
			if (newMessageFromServer.substring(8, 10).equals("ST")) {
				tempModel.setStartGameButtonEnabled();
				//tempMsg.addMessage("We have all players now. Click \"START GAME\" to begin.");
				//return ok(askForStartingGame.render("We have all players now." + "<br>" + "Click \"START GAME\" to begin."));
			} else {
				tempMsg.addMessage("Player " + newMessageFromServer.substring(8, 9)
						+ " has joined the game. Wainting for "
						+ newMessageFromServer.substring(10, 11) + " player(s).");
				//return ok(index.render(tempCore.getMessageToDisplay()));
			}
			break;
		case "03":
			System.out.println("New hand: " + newMessageFromServer);
			// transfer of the cards.
			// Content: ColourOfTheCard,ValueOfTheCard/C,V/C,V/C,V;
			// example: <2><03><1,7/4,11/3,8/2,10>
			tempModel.setHand(tempCore.getParameters());
			tempMsg.addMessage("Your hand: " + tempModel.getCardName(0) + ", " + tempModel.getCardName(1) + ", " + tempModel.getCardName(2) + ", " + tempModel.getCardName(3) + ".");
			break;
		case "04":
			// who has: dealer button, small blind, big blind
			// Content:
			// IDPlayerWithDealerButtonNow,IDPlayerWithSmallBlindNow,IDPlayerWithBigBlindNow
			// example: <2><04><0,1,2>
			System.out.println("<04> dealer button: " + newMessageFromServer);
			tempModel.setDealer(newMessageFromServer.substring(8, 9));
			tempModel.setWhoHasSmallBlind(newMessageFromServer.substring(10, 11));
			tempModel.setWhoHasBigBlind(newMessageFromServer.substring(12, 13));
			tempModel.setMaxBetNow(tempModel.getBigBlind());
			// System.out.println("dealer: " + model.getDealer());
			// System.out.println("big blind: " + model.getWhoHasBigBlind());
			// System.out.println("small blind: " + model.getWhoHasSmallBlind());
			if (tempModel.getDealer().equals(tempModel.getPlayerID())) {
				tempMsg.addMessage("You have dealer button.");
			} else {
				tempMsg.addMessage("Player " + tempModel.getDealer() + " has dealer button.");
			}
			if (tempModel.getWhoHasBigBlind().equals(tempModel.getPlayerID())) {
				tempMsg.addMessage("You have big blind.");
			} else {
				tempMsg.addMessage("Player " + tempModel.getWhoHasBigBlind() + " has big blind.");
			}
			if (tempModel.getWhoHasSmallBlind().equals(tempModel.getPlayerID())) {
				tempMsg.addMessage("You have small blind.");
			} else {
				tempMsg.addMessage("Player " + tempModel.getWhoHasSmallBlind() + " has small blind.");
			}
			if (tempModel.getDealer().equals(tempModel.getPlayerID())) {
				tempMsg.addMessage("Your bet: 0T.");
				tempMsg.addMessage("Your BANKROLL: " + tempModel.getPlayerBankrollNow() + "T");
			} else {
				tempMsg.addMessage("Player " + tempModel.getDealer() + " bet: 0T. BANKROLL: " + tempModel.getBankroll() + "T");
			}
			if (tempModel.getWhoHasBigBlind().equals(tempModel.getPlayerID())) {
				tempMsg.addMessage("Your bet: " + tempModel.getPlayerBetNow() + "T.");
				tempMsg.addMessage("Your BANKROLL: " + tempModel.getPlayerBankrollNow() + "T");
				tempMsg.addMessage("Player " + tempModel.getWhoHasSmallBlind() + " bet: " + tempModel.getSmallBlind() + "T. BANKROLL: " + (Integer.parseInt(tempModel.getBankroll()) - Integer.parseInt(tempModel.getSmallBlind())) + "T");
			} else if (tempModel.getWhoHasSmallBlind().equals(tempModel.getPlayerID())) {
				tempMsg.addMessage("Player " + tempModel.getWhoHasBigBlind() + " bet: " + tempModel.getBigBlind() + "T. BANKROLL: " + (Integer.parseInt(tempModel.getBankroll()) - Integer.parseInt(tempModel.getBigBlind())) + "T");
				tempMsg.addMessage("Your bet: " + tempModel.getPlayerBetNow() + "T.");
				tempMsg.addMessage("Your BANKROLL: " + tempModel.getPlayerBankrollNow() + "T");
			} else {
				tempMsg.addMessage("Player " + tempModel.getWhoHasBigBlind() + " bet: " + tempModel.getBigBlind() + "T. BANKROLL: " + (Integer.parseInt(tempModel.getBankroll()) - Integer.parseInt(tempModel.getBigBlind())) + "T");
				tempMsg.addMessage("Player " + tempModel.getWhoHasSmallBlind() + " bet: " + tempModel.getSmallBlind() + "T. BANKROLL: " + (Integer.parseInt(tempModel.getBankroll()) - Integer.parseInt(tempModel.getSmallBlind())) + "T");
			}
			tempMsg.addMessage("Now max bet is " + tempModel.getMaxBetNow() + "T.");
			break;
		case "05":
			// Betting
			System.out.println("BettingRound: " + newMessageFromServer);
			switch(newMessageFromServer.substring(8, 10)) {
			case "ST":
				tempModel.setBettingRound(newMessageFromServer);
				System.out.println("BettingRound: "
						+ tempModel.getBettingRound());
				switch (tempModel.getBettingRound()) {
				case "0":
					tempMsg.addMessage("First betting round has started. Wait for your turn.");
					break;
				case "1":
					tempMsg.addMessage("Second betting round has started. Wait for your turn.");
					break;
				case "2":
					tempMsg.addMessage("Third betting round has started. Wait for your turn.");
					break;
				case "3":
					tempMsg.addMessage("Last betting round has started. Wait for your turn.");
					break;
				}
				break;
			case "WD":
				switch (tempModel.getBettingRound()) {
				case "0":
					tempMsg.addMessage("First betting round. Choose option to bet.");
					tempModel.enableBettingButtons();
					break;
				case "1":
					tempMsg.addMessage("Second betting round. Choose option to bet.");
					tempModel.enableBettingButtons();
					break;
				case "2":
					tempMsg.addMessage("Third betting round. Choose option to bet.");
					tempModel.enableBettingButtons();
					break;
				case "3":
					tempMsg.addMessage("Last betting round. Choose option to bet.");
					tempModel.enableBettingButtons();
					break;
				}
				break;
//				Message with action of active player; it will be sent to all Clients.
			case "CH":
				tempModel.setIDOfPlayerWhoPlayed(newMessageFromServer.substring(11, 12));
				if (tempModel.getPlayerID().equals(tempModel.getIDOfPlayerWhoPlayed())) {
					tempModel.disableBettingButtons();
					tempMsg.addMessage("You played Check.");
					System.out.println("aaa You played Check.");
				} else {
					tempMsg.addMessage("Player " + tempModel.getIDOfPlayerWhoPlayed() + " played Check.");
					System.out.println("aaa Player " + tempModel.getIDOfPlayerWhoPlayed() + " played Check.");
				}
				break;
			case "BE":
				tempModel.setIDOfPlayerWhoPlayed(newMessageFromServer.substring(11, 12));
				tempModel.setBetValueOfPlayerWhoPlayed(newMessageFromServer.split(",")[2]);
				tempModel.setBankrollOfPlayerWhoPlayed(newMessageFromServer.split(",")[3]);
				tempModel.setWasBetPlayed(true);
				if (tempModel.getWhoHasSmallBlind().equals(tempModel.getIDOfPlayerWhoPlayed())) {
					tempModel.setMaxBetNow(Integer.toString(tempModel.getBetValueOfPlayerWhoPlayed() + Integer.parseInt(tempModel.getSmallBlind())));
				} else if (tempModel.getWhoHasBigBlind().equals(tempModel.getIDOfPlayerWhoPlayed())) {
					tempModel.setMaxBetNow(Integer.toString(tempModel.getBetValueOfPlayerWhoPlayed() + Integer.parseInt(tempModel.getBigBlind())));
				} else {
					tempModel.setMaxBetNow(Integer.toString(tempModel.getBetValueOfPlayerWhoPlayed()));
				}
				System.out.println("aaa NEW MAX BET: " + tempModel.getMaxBetNow());
				if (tempModel.getPlayerID().equals(tempModel.getIDOfPlayerWhoPlayed())) {
					if (tempModel.getWhoHasSmallBlind().equals(tempModel.getPlayerID())) {
						tempModel.setPlayerBetNow(Integer.toString(tempModel.getBetValueOfPlayerWhoPlayed() + Integer.parseInt(tempModel.getSmallBlind())));
					} else if (tempModel.getWhoHasBigBlind().equals(tempModel.getPlayerID())) {
						tempModel.setPlayerBetNow(Integer.toString(tempModel.getBetValueOfPlayerWhoPlayed() + Integer.parseInt(tempModel.getBigBlind())));
					} else {
						tempModel.setPlayerBetNow(Integer.toString(tempModel.getBetValueOfPlayerWhoPlayed()));
					}
					tempModel.setPlayerBankrollNow(Integer.parseInt(tempModel.getBankrollOfPlayerWhoPlayed()));
					tempModel.disableBettingButtons();
					tempMsg.addMessage("You played Bet. Your BANKROLL: " + tempModel.getPlayerBankrollNow() + "T");
					System.out.println("aaa You played Bet.");
				} else {
					tempMsg.addMessage("Player " + tempModel.getIDOfPlayerWhoPlayed() + " played Bet.");
					System.out.println("aaa Player " + tempModel.getIDOfPlayerWhoPlayed() + " played Bet.");
				}
				tempMsg.addMessage("MAX BET: " + tempModel.getMaxBetNow() + "T");
				//disable betButton
				//enable raiseButton 
				break;
			case "RA":
				tempModel.setIDOfPlayerWhoPlayed(newMessageFromServer.substring(11, 12));
				tempModel.setBetValueOfPlayerWhoPlayed(newMessageFromServer.split(",")[2]);
				tempModel.setBankrollOfPlayerWhoPlayed(newMessageFromServer.split(",")[3]);
				tempModel.setWasRaisePlayed(true);
				if (tempModel.getWhoHasSmallBlind().equals(tempModel.getIDOfPlayerWhoPlayed())) {
					tempModel.setMaxBetNow(Integer.toString(tempModel.getBetValueOfPlayerWhoPlayed() + Integer.parseInt(tempModel.getSmallBlind())));
				} else if (tempModel.getWhoHasBigBlind().equals(tempModel.getIDOfPlayerWhoPlayed())) {
					tempModel.setMaxBetNow(Integer.toString(tempModel.getBetValueOfPlayerWhoPlayed() + Integer.parseInt(tempModel.getBigBlind())));
				} else {
					tempModel.setMaxBetNow(Integer.toString(tempModel.getBetValueOfPlayerWhoPlayed()));
				}
				System.out.println("aaa NEW MAX BET: " + tempModel.getMaxBetNow());
				if (tempModel.getPlayerID().equals(tempModel.getIDOfPlayerWhoPlayed())) {
					if (tempModel.getWhoHasSmallBlind().equals(tempModel.getPlayerID())) {
						tempModel.setPlayerBetNow(Integer.toString(tempModel.getBetValueOfPlayerWhoPlayed() + Integer.parseInt(tempModel.getSmallBlind())));
					} else if (tempModel.getWhoHasBigBlind().equals(tempModel.getPlayerID())) {
						tempModel.setPlayerBetNow(Integer.toString(tempModel.getBetValueOfPlayerWhoPlayed() + Integer.parseInt(tempModel.getBigBlind())));
					} else {
						tempModel.setPlayerBetNow(Integer.toString(tempModel.getBetValueOfPlayerWhoPlayed()));
					}
					tempModel.setPlayerBankrollNow(Integer.parseInt(tempModel.getBankrollOfPlayerWhoPlayed()));
					tempModel.disableBettingButtons();
					tempMsg.addMessage("You played Raise. Your BANKROLL: " + tempModel.getPlayerBankrollNow() + "T");
					System.out.println("aaa You played Raise.");
				} else {
					tempMsg.addMessage("Player " + tempModel.getIDOfPlayerWhoPlayed() + " played Raise.");
					System.out.println("aaa Player " + tempModel.getIDOfPlayerWhoPlayed() + " played Raise.");
				}
				tempMsg.addMessage("MAX BET: " + tempModel.getMaxBetNow() + "T");
				break;
			case "CA":
				tempModel.setIDOfPlayerWhoPlayed(newMessageFromServer.substring(11, 12));
				tempModel.setBankrollOfPlayerWhoPlayed(newMessageFromServer.split(",")[3]);
//				System.out.println("aaa Bankroll OfPlayerWhoPlayed \"Call\": " + model.getBankrollOfPlayerWhoPlayed());
				if (tempModel.getPlayerID().equals(tempModel.getIDOfPlayerWhoPlayed())) {
					tempModel.setPlayerBetNow(Integer.toString(tempModel.getMaxBetNow()));
					tempModel.setPlayerBankrollNow(Integer.parseInt(tempModel.getBankrollOfPlayerWhoPlayed()));
					tempModel.disableBettingButtons();
					tempMsg.addMessage("You played Call. Your BANKROLL: " + tempModel.getPlayerBankrollNow() + "T");
					System.out.println("aaa You played Call.");
				} else {
					tempMsg.addMessage("Player " + tempModel.getIDOfPlayerWhoPlayed() + " played Call.");
					System.out.println("aaa Player " + tempModel.getIDOfPlayerWhoPlayed() + " played Call.");
				}
				break;
			case "FO":
				tempModel.setIDOfPlayerWhoPlayed(newMessageFromServer.substring(11, 12));
				if (tempModel.getPlayerID().equals(tempModel.getIDOfPlayerWhoPlayed())) {
					tempModel.disableBettingButtons();
					tempMsg.addMessage("You played Fold.");
					System.out.println("aaa You played Fold.");
				} else {
					tempMsg.addMessage("Player " + tempModel.getIDOfPlayerWhoPlayed() + " played Fold.");
					System.out.println("aaa Player " + tempModel.getIDOfPlayerWhoPlayed() + " played Fold.");
				}
				break;
			case "AL":
				tempModel.setIDOfPlayerWhoPlayed(newMessageFromServer.substring(11, 12));
				tempModel.setBetValueOfPlayerWhoPlayed(newMessageFromServer.split(",")[2]);
				//bankroll should be 0 instead of being equal to betvalueofplayerwhoplayed
//				System.out.println("aaa Bet Value OfPlayerWhoPlayed \"Call\": " + model.getBetValueOfPlayerWhoPlayed());
//				System.out.println("aaa Bankroll OfPlayerWhoPlayed \"Call\": " + model.getBankrollOfPlayerWhoPlayed());
				if (tempModel.getPlayerID().equals(tempModel.getIDOfPlayerWhoPlayed())) {
					tempModel.setPlayerBetNow(Integer.toString((tempModel.getBetValueOfPlayerWhoPlayed()+tempModel.getPlayerBetNow())));
					tempModel.setPlayerBankrollNow(0);
					tempModel.disableBettingButtons();
					tempMsg.addMessage("You played All-in. Your BANKROLL: " + tempModel.getPlayerBankrollNow() + "T");
					System.out.println("aaa You played All-in.");
				} else {
					tempMsg.addMessage("Player " + tempModel.getIDOfPlayerWhoPlayed() + " played All-in.");
					System.out.println("aaa Player " + tempModel.getIDOfPlayerWhoPlayed() + " played All-in.");
				}
				break;
			}
			break;
		case "07":
			//Changing - QUESTIONS of ROOM and messages of the ROOM.
			System.out.println("Exchange: " + newMessageFromServer);
			switch(newMessageFromServer.substring(8, 10)) {
			case "ST":
				tempMsg.addMessage("Card exchange has started. Wait for your turn.");
				break;
			case "WD":
				tempMsg.addMessage("Card exchange. Choose cards to exchange and click EXCHANGE.");
				tempModel.enableExchangeButton();
				break;
			default:
//				displays how many cards has another player changed
//				Message with action of active player; it will be sent to all Clients.
//				<RoomID><07><NrOfChangingCards,IDOfPlayer>
				tempModel.setPlayerIDWhoExchanged(newMessageFromServer);
				tempModel.setNrOfExchangedCards(newMessageFromServer);
				if (tempModel.getPlayerID().equals(tempModel.getPlayerIDWhoExchanged())) {
					tempModel.disableExchangeButton();
					tempMsg.addMessage("You exchanged " + tempModel.getNrOfExchangedCards() + " cards.");
					System.out.println("aaa You exchanged " + tempModel.getNrOfExchangedCards() + " cards.");
				} else {
					tempMsg.addMessage("Player " + tempModel.getPlayerIDWhoExchanged() + " exchanged " + tempModel.getNrOfExchangedCards() + " cards.");
					System.out.println("aaa Player " + tempModel.getPlayerIDWhoExchanged() + " exchanged " + tempModel.getNrOfExchangedCards() + " cards.");
				}
			}
			break;
		case "09":
//			//Winner announce.
			System.out.println("Winner!: " + newMessageFromServer);
			switch(newMessageFromServer.substring(8, 10)) {
			case "WI":
				tempModel.setNumberOfWinners(newMessageFromServer);
				tempModel.setWinnerID(newMessageFromServer);
				tempModel.setWinningAmount(newMessageFromServer);
				if (tempModel.getWinnerID().equals(tempModel.getPlayerID())) {
					tempModel.setPlayerBankrollAfterWinning(Integer.parseInt(tempModel.getWinningAmount()));
					tempMsg.addMessage("Winner!: You won " + tempModel.getWinningAmount() + "T. BANKROLL: " + tempModel.getPlayerBankrollNow() + "T");
				} else {
					tempMsg.addMessage("Winner!: Player " + tempModel.getWinnerID() + " won " + tempModel.getWinningAmount() + "T.");
				}
				System.out.println("Winner!: Player " + tempModel.getWinnerID() + " won " + tempModel.getWinningAmount() + "T.");
				break;
			case "DR":
				tempModel.setNumberOfWinners(newMessageFromServer);
				tempModel.setWinnersID(newMessageFromServer);
				tempModel.setWinningAmount(newMessageFromServer);
				for(int i=0; i<tempModel.getNumberOfWinners(); i++){
					String winnerID = tempModel.getWinnersID().substring(i, i+1);
					if(winnerID.equals(tempModel.getPlayerID())) {
						tempModel.setPlayerBankrollAfterWinning(Integer.parseInt(tempModel.getWinningAmount()));
						tempMsg.addMessage("Winner!: You won " + tempModel.getWinningAmount() + "T. BANKROLL: " + tempModel.getPlayerBankrollNow() + "T");
					}
				}
				tempMsg.addMessage("Winners!: Players " + tempModel.getWinnersID() + " won " + tempModel.getWinningAmount() + "T.");
				System.out.println("Winners!: Players " + tempModel.getWinnersID() + " won " + tempModel.getWinningAmount() + "T.");
				break;
			}
			break;
		case "10":
//			//next round?
			System.out.println("aaa Next round?: " + newMessageFromServer);
			switch(newMessageFromServer.substring(8, 10)) {
			case "AR":
				tempModel.enableNextGameButton();
				tempMsg.addMessage("Do you want to play again?");
				break;
			case "LO":
				tempModel.disableNextGameButton();
				tempModel.setLooserID(newMessageFromServer);
				tempMsg.addMessage("Player " + tempModel.getLooserID() + " have lost. Thanks for game. See you later!");
				break;
			case "SN":
				tempModel.disableNextGameButton();
				tempMsg.addMessage("Other player(s) do not want to play anymore. Thanks for game. See you later!");
				break;
			}
			break;
		case "30":
			//Errors
			System.out.println("aaa ERROR: " + newMessageFromServer);
			switch(newMessageFromServer.substring(8, 10)) {
			case "01":
				tempMsg.addMessage("Client, send this message once again, please.");
				break;
			case "02":
				tempMsg.addMessage("EPIC FAILURE.  Do not be afraid - the Server will create a new Room, and will be joined them automatically.");
				break;
			}
			break;
		}
		//dodaje wszystkie wiadomosci odebrane od serwera
		//tempMsg.addMessage(newMessageFromServer);
	}
	
	//wyswietlanie wiadomosci
	public static Result forwardMessages() {
    	if(session("playerNr") == null) {
    		return redirect(routes.Application.index());
    	}
    	int playerNr = Integer.parseInt(session("playerNr"));
    	Messages tempMsg = messages.getMessages(playerNr);
    	String messageToDisplay = tempMsg.mergeAllToOne();
		return ok(forwardMessages.render(messageToDisplay));
	}

	public static void nullPointerExceptionOccured() {
		// TODO Auto-generated method stub
		
	}

	public static void connectionFailureExceptionOccured() {
		// TODO Auto-generated method stub
		
	}
}
